<?php
namespace app\Backend\controller;;

use think\Controller;
use app\Backend\model\Admin;

class Login extends Controller
{
    public function index(){
        if(request()->isPost()) {
            $admin = new Admin();
            $data = input('post.');
            if(!$this->check($data['captcha'])){
                return json(array('code' => 0, 'msg' => '验证码错误'));
            }
            $num = $admin->login($data);
            if($num == 1){
                return json(['code' => 1, 'msg' => '登录成功!', 'url' => url('index/index')]);
            }else {
                return json(array('code' => 0, 'msg' => '用户名或者密码错误，重新输入!', 'url'=>'/reload') );
            }
        }else{
            if (session('aid')) { //已登录后台,直接跳转进后台
                $this->redirect('index/index');
            }else{
                return $this->fetch();//未登录后台,显示登录界面
            }

        }
    }
    public function check($code){
       return captcha_check($code);
    }
    //退出登陆
    public function logout(){
        session(null);
        $this->redirect('login/index');
    }
}